package com.pumpkin.asyncdemo.controller;

import com.pumpkin.asyncdemo.component.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private Task task;

    @RequestMapping("/hello")
    public String testAsync() throws ExecutionException, InterruptedException {
        // System.out.println("111");
        // System.out.println("---");
        // Thread.sleep(2000);
        // System.out.println("===");
        CompletableFuture<?> future = CompletableFuture.supplyAsync(() -> {
            try {
                task.doTaskOne("pumpkin");
                task.doTaskTwo();
                task.doTaskThree();
                return "controller执行完成";
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
                return "error";
            }
        });
        // task.doTaskOne();
        // System.out.println("final:" + future.get());
        return "hello world!111";
    }
}
